package com.project.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.mapper.IUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class UserService implements com.project.service.IUserService {
    @Autowired
    private IUserMapper userMapper;
    @Override
    public List<UserBean> findAll() {
        return userMapper.selectList(null);
    }

    @Override
    public UserBean login(String userName, String pwd) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name",userName);
        qw.eq("u_pwd",pwd);

        UserBean user = userMapper.selectOne(qw);

        return user;
    }

    @Override
    public boolean addUser(UserBean userBean) {
        QueryWrapper<UserBean> qw = new QueryWrapper<>();
        qw.eq("u_name",userBean.getUserName());

        UserBean tempUser = userMapper.selectOne(qw);
        if(tempUser != null){
            return false;
        }

        userMapper.insert(userBean);
        return true;
    }



    @Override
    public boolean delUser(int id,UserBean userBean) {
        if(id == userBean.getId()){
            return false;
        }

        userMapper.deleteById(id);

        return true;
    }

    @Override
    public UserBean findById(int id) {
        return userMapper.selectById(id);
    }
    @Override
    public void updateUserPwd(int id,String pwd) {
        UserBean user = new UserBean();
        user.setId(id);
        user.setPwd(pwd);
        userMapper.updateById(user);
    }

    @Override
    public void updateGrade(int id, String level) {
        UserBean user = new UserBean();
        user.setId(id);
        user.setGrade(level);
        userMapper.updateById(user);
    }
}
